package io.terminus.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.terminus.spzx.manager.service.ProductSpecService;
import io.terminus.spzx.model.entity.product.ProductSpec;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 商品规格Controller
 * @Date 2024-10-10
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "商品规格接口")
@RequestMapping(value = "/admin/product/productSpec")
public class ProductSpecController {

    private final ProductSpecService productSpecService;

    /**
     * 分页查询商品规格
     *
     * @param page  当前页
     * @param limit 每页记录数
     * @return 分页查询结果
     */
    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<ProductSpec>> findByPage(@PathVariable Integer page, @PathVariable Integer limit) {
        PageInfo<ProductSpec> pageInfo = productSpecService.findByPage(page, limit);

        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询商品规格列表
     *
     * @return 商品规格列表
     */
    @GetMapping("findAll")
    public Result<List<ProductSpec>> findAll() {
        List<ProductSpec> list = productSpecService.findAll();

        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存商品规格
     *
     * @param productSpec 商品规格
     * @return 保存结果
     */
    @PostMapping("save")
    public Result<Void> save(@RequestBody ProductSpec productSpec) {
        productSpecService.save(productSpec);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 更新商品规格
     *
     * @param productSpec 商品规格
     * @return 更新结果
     */
    @PutMapping("updateById")
    public Result<Void> updateById(@RequestBody ProductSpec productSpec) {
        productSpecService.updateById(productSpec);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除商品规格
     *
     * @param id 商品规格ID
     * @return 删除结果
     */
    @DeleteMapping("/deleteById/{id}")
    public Result<Void> removeById(@PathVariable Long id) {
        productSpecService.deleteById(id);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
